﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPivotGrid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class HaftalikGelisler : XForm
    {
        public HaftalikGelisler()
        {
            InitializeComponent();
        }

        BindingSource bs2;
        private void HaftalikGelisler_Load(object sender, EventArgs e)
        {
            DataSet d = ExecuteDataSet("GetHaftalikYukleme");
            bs2 = new BindingSource(d, "Table");
            InitGridView(gv, bs2);
            //DrillDownGrid(gv, bs2);
            gv.FormatAsNumber(1, "GelecekQty");
            gv.AddSummary("GelecekQty");
            gv.RowCellStyle += gv_RowCellStyle;
            InitPivot(pvt, bs2);
            gv.BestFitColumns();

            pvt.CustomAppearance += new PivotCustomAppearanceEventHandler(pvt_CustomAppearance);
            pvt.FocusedCellChanged += new EventHandler(pvt_FocusedCellChanged);
            pvt.CellClick += new PivotCellEventHandler(pvt_CellClick);

            splitContainerControl2.AutoSize();
            chkGruplama.Edit.EditValueChanging += Edit_EditValueChanging;
            chkGruplama.EditValue = "Customer";
        }


        void ExportAction(object sender, EventArgs e)
        {
            PivotGridControl pivot = ((DXMenuItem)sender).Tag as PivotGridControl;
            if (pivot == null) return;

            pvt.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            ExportPivot(pvt);

            //pvt.ShowPrintPreview();
        }

        PivotGridFieldBase[] pgf2;
        void Edit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            pvt.Groups.Clear();
            if (pvt.Fields.Contains(pvt.Fields["Vendor"]))
                pvt.Fields.Remove(pvt.Fields["Vendor"]);
            if (pvt.Fields.Contains(pvt.Fields["Customer"]))
                pvt.Fields.Remove(pvt.Fields["Customer"]);
            if (pvt.Fields.Contains(pvt.Fields["Brand"]))
                pvt.Fields.Remove(pvt.Fields["Brand"]);

            switch (e.NewValue.ToString())
            {
                case "Customer":
                    pgf2 = new PivotGridFieldBase[2];
                    pgf2[0] = AddNewField(pvt, "Customer", "Müşteri", PivotArea.RowArea);
                    //((PivotGridField)pgf2[0]).FieldEdit = RI(Rp.V_Customer);

                    pgf2[1] = AddNewField(pvt, "Brand", "Marka", PivotArea.RowArea);
                    //((PivotGridField)pgf2[1]).FieldEdit = RI(Rp.Brand);

                    pgf2[0].ExpandedInFieldsGroup = false;
                    pvt.Groups.Add(pgf2, "BrandCustomer");
                    break;

                case "Vendor":
                    pgf2 = new PivotGridFieldBase[1];
                    pgf2[0] = AddNewField(pvt, "Vendor", "Üretici", PivotArea.RowArea);
                    //((PivotGridField)pgf2[0]).FieldEdit = RI(Rp.Vendor);
                    pvt.Groups.Add(pgf2, "Uretici");
                    break;

            }
            pvt.BestFit();
        }

        void pvt_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }

        private PivotGridField AddNewField(PivotGridControl pvt, string FieldName, string Caption, PivotArea pa)
        {
            PivotGridField fd = new PivotGridField();
            fd.FieldName = FieldName;
            fd.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            fd.CellFormat.FormatString = "#,###.##";
            fd.SetAreaPosition(pa, 0);
            pvt.Fields.Add(fd);
            fd.Name = "_" + FieldName;
            fd.Caption = Caption;
            return fd;
        }

        private void InitPivot(PivotGridControl pvt, BindingSource bs)
        {
            //pvt = new PivotGridControl();
            pvt.Groups.Clear();
            pvt.Fields.Clear();
            pvt.DataSource = bs;
            pvt.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
            pvt.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
            pvt.PopupMenuShowing += pvt_PopupMenuShowing;
            pvt.BeginUpdate();

            AddNewField(pvt, "ETAweek", "ETAweek", PivotArea.ColumnArea);
            //AddNewField(pvt, "ETAyear", "ETAyear", PivotArea.ColumnArea);

            PivotGridFieldBase[] pgf2 = new PivotGridFieldBase[2];
            pgf2[0] = AddNewField(pvt, "Customer", "Müşteri", PivotArea.RowArea);
            //((PivotGridField)pgf2[0]).FieldEdit = RI(Rp.V_Customer);

            pgf2[1] = AddNewField(pvt, "Brand", "Marka", PivotArea.RowArea);
            //((PivotGridField)pgf2[1]).FieldEdit = RI(Rp.Brand);

            pgf2[0].ExpandedInFieldsGroup = false;
            pvt.Groups.Add(pgf2, "BrandCustomer");

            AddNewField(pvt, "ShipMethodId", PivotArea.FilterArea).FieldEdit = RI(Rp.ShipMethod);
            AddNewField(pvt, "Vendor", PivotArea.FilterArea);   //.FieldEdit = RI(Rp.Vendor);
            AddNewField(pvt, "ETA", PivotArea.FilterArea).FieldEdit = new DevExpress.XtraEditors.Repository.RepositoryItemDateEdit();

            AddNewField(pvt, "GelecekQty", "Metraj", PivotArea.DataArea);

            //.Width = 40;  .Visible = true;
            //    fld.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;

            foreach (PivotGridField pgf in pvt.Fields)            
                if (pgf.FieldName.Contains("Customer"))
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }
            
            pvt.EndUpdate();
            pvt.BestFit();
        }

        void pvt_PopupMenuShowing(object sender, DevExpress.XtraPivotGrid.PopupMenuShowingEventArgs e)
        {
            //if (e.MenuType == DevExpress.XtraPivotGrid.PivotGridMenuType.HeaderArea)
            {
                DXMenuItem item = new DXMenuItem("Export to Excel", new EventHandler(ExportAction));
                item.Tag = sender;
                e.Menu.Items.Add(item);
            }
        }


        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "GelecekQty")
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void pvt_CellClick(object sender, PivotCellEventArgs e)
        {
            CellChanged();
        }

        void pvt_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        bool changing = false;

        private void CellChanged()
        {
            if (!changing)
                try
                {
                    changing = true;
                    PivotDrillDownDataSource pds = pvt.Cells.GetFocusedCellInfo().CreateDrillDownDataSource();
                    gc.DataSource = pds;
                    pvt.Invalidate();
                    gv.BestFitColumns();
                }
                finally
                {
                    changing = false;
                }
        }

    }
}
